﻿using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using System.Linq;
using QDT.Common;
using QDT.Core;
using QDT.Core.Domain;
using QDT.Core.Members;
using QDT.Core.Orders;
using QDT.Web.Code;
using QDT.Web.Models;


namespace QDT.Web.Controllers {

    public partial class UserController {

        public ActionResult Money(int? t,int? page)
        {
            const int pageSize = 5;
            t = t ?? 0;
            var pageNumber = page ?? 1;
            const int itrand = (int)UserTransLog.TradeType.Charge;

            const int ctrand1 = (int)UserTransLog.TradeType.CarrayMoneyToLock;
            const int ctrand2 = (int)UserTransLog.TradeType.CarrayLockMoneyDefray;
            const int ctrand3 = (int)UserTransLog.TradeType.CarrayMoneyToLock;

            var records = DbProvider.D<UserTransLog>().Where(x => x.UserID == User.ID);

            if(t==1) {
                records = records.Where(x => x.InnerTrade == itrand);
            }
            else if (t == 2)
            {
                records =
                    records.Where(x => x.InnerTrade == ctrand1 || x.InnerTrade == ctrand2 || x.InnerTrade == ctrand3);
            }
            else
            {
                records =
                    records.Where(x => x.InnerTrade == ctrand1 || x.InnerTrade == ctrand2 || x.InnerTrade == ctrand3||x.InnerTrade == itrand);
            }

            var userAccount = DbProvider.GetByID<User>(User.ID).UserAccount;

            ViewData["UserAccount"] = userAccount;

            return View(records.OrderByDescending(x=>x.ID).ToPagedList(pageNumber-1,pageSize));
        }


        public ActionResult MoneyIncharge() {
            if (!User.IDCardPassed) return Error("", "请先实名认证", Url.Action("BindIdCard"));
            var chargelist = DbProvider.D<ChargeOrder>().Where(x => x.UserID == User.ID).OrderByDescending(x=>x.ID).ToPagedList(0,3);
            ViewBag.chargelist = chargelist;
            return View(chargelist);
        }

        [HttpPost]
        public ActionResult PostMoneyIncharge(decimal money=0) {
            if (!User.IDCardPassed) return Error("请先实名认证");

            if (money <= 0) return View("MoneyInchargeError", new MessageModel(){Message = "金额错误"});

            var user = _userService.GetUserByID(User.ID);

            var order=OrderService.CreateChargeOrder(user, money, "AllInPayPayment", Request.Params["bank_id"]);

            return RedirectToAction("Pay", "Payment",new {orderid=order.ID});

        }

        public ActionResult MoneyCarry()
        {
            if (!User.IDCardPassed) return Error("", "请先实名认证", Url.Action("BindIdCard"));
            var userbanks = _userService.GetUserBank(User.ID);
            ViewData["UserBank"] = userbanks;
            return View(User);
        }

        [HttpPost]
        public ActionResult PostMoneyCarry(UserCarrayModel model)
        {
            if (!ModelState.IsValid) return Error();
            if (User.ID != model.UserId) return Error("信息错误");
            if (!User.IDCardPassed) return Error("请先实名认证");
            
            if (model.UserBank == 0)
            {
                return Error("请选择银行卡！");
            }
            if ((model.Money * 100) % 10 != 0)
            {
                return Error("请输入0.1元倍数的金额！");
            }
            if (!User.SafePassword.Equals(model.SafePassword.Hash()))
            {
                return Error("安全密码错误");
            }
            UserBank bank = _userService.GetUserBankById(model.UserBank);
            if (bank == null || bank.UserId != User.ID)
            {
                return Error("请选择银行卡！");
            }
            User user = _userService.GetUserByID(model.UserId);
            if (user.UserAccount.CarryAbleMoney < model.Money)
            {
                return Error("提现金额大于账户可提现余额！");
            }
            var carray = new UserCarry(user, model.Money, bank.BankId, bank.BankCard.Replace(" ", ""), bank.BankZone, bank.RegionLv1, bank.RegionLv2,
                                       bank.RegionLv3, bank.RegionLv4);

            _userService.CreateCarry(carray);

            return Success("提现申请提交成功！");
        }
    }
}